const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function getUserLocation(){
  // var that = this;
  wx.getLocation({
    type: 'wgs84',
    success: function (res) {
      console.log(res)
      // queryWeather(res);
      var location = res.latitude + "," + res.longitude
      wx.setStorageSync("location", location);
      console.info(location)
    },
  })
}

function queryWeather(res) {
  // res.latitude = 34.74810791015625;
  // res.longitude = 113.59202575683594;
  // var that = this;
  wx.request({
    url: 'https://jisutqybmf.market.alicloudapi.com/weather/query',
    header: {
      "Authorization": "APPCODE b3ac862eeb284816b420cf88c990fbbc"
    },
    data: {
      'location': res.latitude + "," + res.longitude
    },
    method: "GET",
    success: function (resRequest) {

      console.info("HTTP - GET")
      console.info(resRequest)

      if (resRequest.statusCode === 200 && resRequest.data.status == 0) {
        var result = resRequest.data.result;
        wx.setStorageSync("weather", result);
        console.info(result)
        var location = res.latitude + "," + res.longitude
        wx.setStorageSync("location", location);
        console.info(location)
      }
    }
  })
}

module.exports = {
  formatTime: formatTime,
  getUserLocation: getUserLocation,
  queryWeather: queryWeather
}
